

** GET BANK PRE-PERIOD SHARES OF COAL LENDING BY BORROWER CONTINENT
use ../Intermediate/bank_lending_panel, clear 
keep BankID year debt debt_eur debt_asia debt_na
keep if inrange(year,2009,2013)
collapse (sum) debt debt_*, by(BankID)
gen frac_debt_eur_pre = debt_eur/debt 
gen frac_debt_asia_pre = debt_asia/debt 
gen frac_debt_na_pre = debt_na/debt
foreach var of var frac_* {
	replace `var' = 0 if missing(`var')
}
egen coal_total_pre = total(debt)
gen bank_share_coal_pre = debt/coal_total_pre

keep BankID frac_* bank_share_coal_pre
tempfile pre_geo 
save `pre_geo'


** Now get bank relationship weights with coal borrowers
use ../Intermediate/transaction_lender_clean, clear
joinby transaction_id using ../Intermediate/transaction_clean
gen year = yofd(date_trans)
drop if missing(year)
drop if missing(BankID)
drop if year >= 2022
drop if amount_trans_lender_usdm < 0

collapse (sum) amount_trans_lender_usdm, by(BankID borrower_id year)

egen bank_coal_year_amt = total(amount_trans_lender_usdm), by(BankID year)

gen pre_period = year >= 2009 & year <= 2013
gen post_period = year >= 2014


gen pre_period_a = year>=2005 & year <= 2009
gen pre_period_b = year>=2010 & year <= 2013

//get betas
preserve 
	collapse (sum) amount_trans_lender_usdm, by(BankID year)
	xtset BankID year
	keep if inrange(year,2005,2013)
	tsfill, full
	replace amount_trans_lender_usdm = 0 if missing(amount_trans_lender_usdm)
	gen logdebt = log(1+amount_trans_lender_usdm)
	tempfile betas0513
	statsby _b, by(BankID) saving(`betas0513'): reg logdebt year if inrange(year,2005,2013)
	tempfile betas0913
	statsby _b, by(BankID) saving(`betas0913'): reg logdebt year if inrange(year,2009,2013)
restore


//get pre period total coal lending by bank
egen bank_coal_pre_amt = total(amount_trans_lender_usdm*pre_period), by(BankID)
egen bank_coal_post_amt = total(amount_trans_lender_usdm*post_period), by(BankID)

egen bank_coal_pre_amt_a = total(amount_trans_lender_usdm*pre_period_a), by(BankID)
egen bank_coal_pre_amt_b = total(amount_trans_lender_usdm*pre_period_b), by(BankID)

gen bank_coal_growth_pre = log(bank_coal_pre_amt_b)-log(bank_coal_pre_amt_a)

//get pre period total coal lending for bank/borrower pairs 
egen bank_firm_pre_amt = total(amount_trans_lender_usdm*pre_period), by(BankID borrower_id)

//get exposure of bank to each firm
gen bank_exp_firm_pre = bank_firm_pre_amt/bank_coal_pre_amt

bys BankID borrower_id: gen bank_firm_cx = _n == 1

//conditional on relationship in pre period, the median coal firm is 2% of banks coal portfolio, mean is 12%
sum bank_exp_firm_pre if bank_firm_cx == 1 & bank_exp_firm_pre > 0, de



egen n_firms_bank_pre = nvals(borrower_id) if pre_period == 1, by(BankID)
bys BankID (n_firms_bank_pre): replace n_firms_bank_pre = n_firms_bank_pre[1]
replace n_firms_bank_pre = 0 if missing(n_firms_bank_pre) //banks that have no coal borrowers in pre period
keep BankID borrower_id n_firms_bank_pre bank_coal_pre_amt bank_coal_post_amt bank_exp_firm_pre bank_coal_growth_pre
duplicates drop 
duplicates report BankID borrower_id



//merge in borrower pretrends
merge m:1 borrower_id using ../Intermediate/borrower_pretrends, keep(3) nogen
rename _b* _b_firm*
//make bank portfolio aggregates of firms 
merge m:1 borrower_id using ../Intermediate/borrower_chars, keep(1 3) nogen

gen coalshare = CoalSharePower
replace coalshare = CoalFracRev if missing(coalshare)

//form bank pre-period portfolio characteristics
collapse (mean) pre_growth_borr log_pre_growth_borr coalshare bank_coal_growth_pre borr_debt_* _b_* CoalSharePower CoalFracRev InstalledCoalPowerCapacity expansion_* coal_industry_* n_firms_bank_pre bank_coal_pre_amt bank_coal_post_amt [aweight=bank_exp_firm_pre], by(BankID)

//merge in pretrend betas
merge m:1 BankID using `betas0513', keep(1 3) nogen
rename _b_year _b_bank_year_0513
drop _b_cons
merge m:1 BankID using `betas0913', keep(1 3) nogen
rename _b_year _b_bank_year_0913
drop _b_cons

merge 1:1 BankID using `pre_geo', keep(1 3) nogen

save ../Intermediate/bank_coal_pretrends, replace


